#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"

#define StudentNum 100//学生人数
#define ScoreNum 10 //成绩个数
#define _CollegeNum 10//学院个数
#define Prefix 1400
int NameL1,NameL2;
int CollegeNum[_CollegeNum];
int PassNum,FailNum,PeopleNum[2];

char *SLName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞仁袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解鹰宗丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟博印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双";
char *SSName="皑蔼霭安鞍岸按昂盎遨翱奥白百佰柏班颁斑邦浜榜胞炮雹宝保豹瀑曝北备倍焙奔本必荜秘筚弼裨辟扁卞汴标彪骠表宾彬斌滨缤槟镔兵丙秉炳并波玻帛泊勃铂博渤箔魄檗卜步埔瓿才材财参灿藏操策曾差察昌长昶畅倡绰超焯晁朝潮彻郴琛澈臣辰晨承盛程驰池炽冲重崇绸畴酬筹楚处川传创棰锤淳赐聪璁丛淙琮璀村存锉达大代岱玳单郸石当铛党宕导岛祷道得德地灯登迪狄笛柢砥递谛滇巅典淀雕顶鼎东栋斗督顿笃度端缎煅锻敦铎恩发法帆藩凡飞斐奋丰风枫峰烽锋逢福阜复赋富盖溉概甘刚岗钢罡港杠高诰革葛铬根庚耿功宫恭巩贡钩构故顾观冠莞管光广归圭贵桂国过海含函涵韩寒汉翰瀚行巷蒿号毫豪貉昊浩皓灏禾合和河赫鹤黑亨恒横衡弘宏泓洪鸿侯厚湖虎浒琥沪淮槐还环洹桓寰焕皇辉晖徽炜回会荟惠火伙豁机矶奇积计记纪技系季际继绩稷加家嘉甲价驾架稼尖坚间俭检简见饯建剑健漳谏践镜鉴键江将奖强交佼皎觉教阶节杰解介界金津进近劲晋京经荆景敬径竞竟靖镜炯赳九韭京居驹枸举巨炬镌隽决诀军均君钧俊郡峻浚骏竣开凯恺慨楷勘看康慷科恪恳铿宽矿奎逵葵魁坤昆来岚览榄郎朗浪烙乐雷镭磊棱冷漓黎礼里俚理锂力历立利沥连涟联炼链良凉梁量粮两亮谅晾辽廖燎列烈猎林临淋磷嶙麟伶灵苓陵聆棂翎岭领令留流陆龙栊胧隆弄楼嵝镂卢钽陆录鹿碌禄路吕旅律率峦鸾銮";

typedef struct Student{
	int ID;
	char Name[8];
	char Sex;
	int College;
	int Score[ScoreNum];
}Student;

int Init();
int CreSore(Student *man);
int CreCollege(Student *man);
int CreSex(Student *man);
int CreName(Student *man);
int CreID(Student *man);
int prit(Student *man);

////////////  Guo S.
int ReadCodeInfo(Student *man, char *filename);		// Guo S. 读取学院名信息
int ReadStudentInfo(Student *man, char *filename);	// Guo S. 读取学生信息
char CollName[_CollegeNum][32]; // Guo S.定义学院名字符串，每个字符数组长度32

int ReadCodeInfo(char man[][32], char *filename)
{
	FILE *fp;
	int i, j;
	char temp;
	fp = fopen(filename, "r");
	if(fp==NULL){
		printf("fun. ReadCodeInfo Error!\n");
		return 1;
	}
	for(j=0;j<_CollegeNum;++j){
		fscanf(fp, "%d",&i);
		fgets(CollName[i-1], 32, fp); // 1号学院存放到字符串CollName[0] 
	}
	fclose(fp);
	return 0;
}

int ReadStudentInfo(Student *man, char *filename)
{
	FILE *fp;
	int i, j;
	char temp;
	fp = fopen(filename, "r");
	if(fp==NULL){
		printf("fun. ReadStudentInfo Error!\n");
		return 1;
	}
	// Guo S. 提示：以下所有 (man+j)->xxx 处，都可以改为：man[j].xxx
	for(j=0;j<StudentNum;++j){
		fscanf(fp, "%d%s%d%d",&(man+j)->ID,(man+j)->Name,&(man+j)->Sex,&(man+j)->College);
		for(i = 0 ; i != ScoreNum ;++i)
			fscanf(fp, "%d",&(man+j)->Score[i]);
	}
	fclose(fp);
	return 0;
}
////////////  Guo S.
int main()
{
	//freopen("StudentInfo.txt","r",stdin);
	//freopen("CodeInfo.txt","r",stdin);
	char name[1024];
	Student pep[StudentNum],temp;
	int i ,j,k;

	ReadStudentInfo(pep, "StudentInfo.txt");
	ReadCodeInfo(CollName,"CodeInfo.txt"); // CollName为全局变量，无需定义

//  ZF
	Init();
	for(i = 0 ; i != StudentNum ; ++i)
	{
		CreSore(&pep[i]);
		CreCollege(&pep[i]);
		CreSex(&pep[i]);
		CreName(&pep[i]);
		CreID(&pep[i]);
	}
	for(i = 0 ;i != 10000 ; ++i)
	{
		j = rand()%StudentNum;
		k = rand()%StudentNum;
		temp = pep[j];
		pep[j] = pep[k];
		pep[k] = temp;//颠倒顺序
	}
	for(i = 0 ; i != StudentNum ; ++i)
	{
		prit(&pep[i]);
	}
	

	return 0;
}


int prit(Student *man)
{
	int i;
	printf("%d %s %d %d ",(*man).ID,(*man).Name,(*man).Sex,(*man).College);
	for(i = 0 ; i != ScoreNum ;++i)
		printf("%d ",(*man).Score[i]);
	printf("\n");
	return 0;
}


int Init()
{
	int i ;
	NameL1 = strlen(SLName);
	NameL2 = strlen(SSName);
	srand(time(NULL));
	for(i = 0 ; i != _CollegeNum ;++i)
		CollegeNum[i] = StudentNum/_CollegeNum;
	FailNum = StudentNum*ScoreNum/10;//不及格率10%
	PassNum = StudentNum*ScoreNum- FailNum;
	PeopleNum[0] = StudentNum/5;//女生人数
	PeopleNum[1] = StudentNum - PeopleNum[0];//男生人数
	return 0;
}
int CreSore(Student *man)
{
	int i,temp,key;
	for(i = 0 ; i != ScoreNum ; ++i)
	{
		key = 1;
		do{
			temp = rand()%101;
			if(temp < 60)
			{
				if(FailNum > 0 )
				{
					key = 0 ;
					--FailNum;
				}
			}
			else
			{
				if(PassNum > 0 )
				{
					key = 0 ;
					--PassNum;
				}
			}
		}while(key);
		(*man).Score[i] = temp;
	}
	return 0;
}
int CreCollege(Student *man)
{
	int i,temp,key;
	key = 1;
	do{
		temp = rand()%10;
		if(CollegeNum[temp]>0)
		{
			CollegeNum[temp]--;
			key = 0;
		}
	}while(key);
	(*man).College = temp;
	return 0;
}
int CreSex(Student *man)
{
	int i,temp,key;
	key = 1;
	do{
		temp = rand()%2;
		if(PeopleNum[temp]>0)
		{
			PeopleNum[temp]--;
			key = 0;
		}
	}while(key);
	(*man).Sex = temp;
	return 0;
}
int CreName(Student *man)
{
	char *name;
	name = (*man).Name;
	int temp,i;
	temp = rand()%NameL1;
	temp-=temp%2;//确保为偶数,汉字是双字节编码
	for(i = 0 ; i !=2 ; ++i)
	{
		name[i] = SLName[temp + i];
	}
	
	temp = rand()%NameL2;
	temp-=temp%2;
	for(i = 2 ; i !=4 ; ++i)
	{
		name[i] = SSName[temp + i - 2];
	}
	
	
	temp = rand()%NameL2;
	temp-=temp%2;
	for(i = 4 ; i !=6 ; ++i)
	{
		name[i] = SSName[temp + i - 4];
	}
	name[6]=0;
	return 0;
}
int CreID(Student *man)
{
	int temp;
	temp = Prefix + (*man).College;
	temp*=100;
	temp+=CollegeNum[(*man).College]+1;
	(*man).ID=temp;
	return 0;
}